import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

class ShopGoodsDetail extends Base {
  constructor(goods) {
    super();

    this.typeId = goods.majorTypeId;
    this.type = goods.majorName;
    this.setGoodsData(goods.goodsDefList);
  }

  setGoodsData(goodsDefList) {
    let goodsList = [];
    for (let i = 0; i < goodsDefList.length; i++) {
      let obj = goodsDefList[i];
      goodsList.push({
        id: obj.goodsDefId,
        defId: obj.goodsDefId,
        name: obj.goodsName,
        modelName: obj.goodsModelName,
        price: obj.goodsModelSalePrice,
        sourcePrice: obj.goodsModelSourcePrice,
        pic: this.getOnePic(obj.goodsPic),
        buyMethod: obj.buyMethod
      });
    }

    this.goodsList = goodsList;
  }
}

export default class ShopGoods extends Base {
  constructor(data) {
    super();

    let shopGoodsList = [];
    let shopGoodsObj = {};
    for (let i = 0; i < data.length; i++) {
      const obj = data[i];
      let goodsList = obj.goodsDefList;
      if (SystemUtil.isEmpty(goodsList)) {
        continue;
      }

      let detail = new ShopGoodsDetail(obj);
      shopGoodsList.push(detail);
      shopGoodsObj[detail.typeId] = detail;
    }

    this.shopGoodsList = shopGoodsList;
    this.shopGoodsObj = shopGoodsObj;

    if (shopGoodsList.length === 0) {
      this.currentGoods = null;
    } else {
      this.currentGoods = shopGoodsList[0];
    }
  }

  setMenu(typeId) {
    let shopGoodsObj = this.shopGoodsObj;
    let shopGoods = shopGoodsObj[typeId];
    this.currentGoods = shopGoods;
  }
}
